
jQuery(document).ready(function () {


//Insert the new preview link.
//id for the preview action div is - preview-action
var previewdiv = jQuery('#preview-action');

//Only do preview logic if you have a valid preview link.
if(previewdiv && previewdiv.length != 0) {
var additionalbuttonhtml = previewdiv.html().replace("post-preview","post-preview2");
additionalbuttonhtml = additionalbuttonhtml.replace("Preview Changes","Active Preview");
//Chop out any text no associated with the anchor tag.
additionalbuttonhtml = additionalbuttonhtml.substring(additionalbuttonhtml.toLowerCase().indexOf('<a'),additionalbuttonhtml.toLowerCase().indexOf('</a>')+4);

var actiondiv = jQuery('#minor-publishing-actions');

actiondiv.append("<div style='margin-top: 3px; float: right;'>"+additionalbuttonhtml+"</div>");

//Hook in the onclick to do an open but keep a reference to the window that was opened for update.
var activelink = jQuery('#post-preview2');

activelink.click(function (e) {
	e.preventDefault();
	//Don't allow this functionality in IE. It just doesn't work well there.
	if(jQuery.browser.msie){
		//For now do an old school alert.
		alert("Active Preview does not support IE browsers, to use functionality please use with another browser");
	} else {
		window.communicator = window.open(this.href,'activepreview','menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
		if(!window.tinyalreadyinit){
			//Hook a listener onto the iframe for changes and send the changes to the content div for the post which wordpress puts into the page.
			//We need to hook into onKeyUp to catch key strokes.
			tinyMCE.activeEditor.onKeyUp.add(function (ed, l) {
			    var jcommunicator = jQuery(window.communicator.document);
			    var post = jcommunicator.find('.post');
			    post.html(ed.getBody().innerHTML);
			}); 
			//We need to hook into onNodeChange to get changes that aren't necessarily related to key strokes (like changing text color etc)
			tinyMCE.activeEditor.onNodeChange.add(function (ed, l) {
			    var jcommunicator = jQuery(window.communicator.document);
			    var post = jcommunicator.find('.post');
			    post.html(ed.getBody().innerHTML);
			}); 
			window.tinyalreadyinit = true;
		}
	}
	return false;
});

} 
});